﻿

Public Class CameraListPanel
    'Loading :
    Private Sub CameraListPanel_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        Control.CheckForIllegalCrossThreadCalls = False
        Me.LoadLCList()
        Me.LoadJCList()
        Me.LoadMCList()
        HeaderTSB_Local.PerformClick()
    End Sub
    'Select List :
    Private Sub SelectTSB(ByVal tsb As ToolStripButton)
        Me.SuspendLayout()
        HeaderTSB_Local.Checked = False
        HeaderTSB_Jpeg.Checked = False
        HeaderTSB_Mjpeg.Checked = False
        tsb.Checked = True
        Me.ResumeLayout()
    End Sub

    'Lists :
    Private _LCList As New List(Of LocalCameraInfos)
    Private _JCList As New List(Of JpegCameraInfos)
    Private _MCList As New List(Of MjpegCameraInfos)
    'Load Lists :
    Private Sub LoadLCList()
        For Each lci As LocalCameraInfos In _LCList
            lci.Dispose()
            lci = Nothing
        Next
        _LCList.Clear()
        Dim fic As New FilterInfoCollection(FilterCategory.VideoInputDevice)
        For Each fi As FilterInfo In fic
            _LCList.Add(New LocalCameraInfos(fi.MonikerString))
        Next
    End Sub
    Private Sub LoadJCList()
        For Each jci As JpegCameraInfos In _JCList
            jci.Dispose()
            jci = Nothing
        Next
        _JCList.Clear()
        Dim jcilf As String = IO.Path.Combine(My.Application.Info.DirectoryPath, "jcams.jcil")
        If IO.File.Exists(jcilf) = False Then
            CamerasWCL.PersistingUtilities.PersistData(New List(Of JpegCameraInfos), jcilf)
        End If
        _JCList = CType(CamerasWCL.PersistingUtilities.LoadData(jcilf), List(Of JpegCameraInfos))
    End Sub
    Private Sub LoadMCList()
        For Each mci As MjpegCameraInfos In _MCList
            mci.Dispose()
            mci = Nothing
        Next
        _MCList.Clear()
        Dim mcilf As String = IO.Path.Combine(My.Application.Info.DirectoryPath, "mcams.mcil")
        If IO.File.Exists(mcilf) = False Then
            CamerasWCL.PersistingUtilities.PersistData(New List(Of MjpegCameraInfos), mcilf)
        End If
        _MCList = CType(CamerasWCL.PersistingUtilities.LoadData(mcilf), List(Of MjpegCameraInfos))
    End Sub
    'Persist Lists :
    Private Sub SaveJCList()
        CamerasWCL.PersistingUtilities.PersistData(_JCList, IO.Path.Combine(My.Application.Info.DirectoryPath, "jcams.jcil"))
    End Sub
    Private Sub SaveMCList()
        CamerasWCL.PersistingUtilities.PersistData(_MCList, IO.Path.Combine(My.Application.Info.DirectoryPath, "mcams.mcil"))
    End Sub
    'View List :
    Private Sub ViewLCList()
        Me.SuspendLayout()
        CamerasList_KLB.Items.Clear()
        For Each lci As LocalCameraInfos In _LCList
            Dim kli As New KryptonListItem(lci.CameraName, "", My.Resources.Img_Camera)
            CamerasList_KLB.Items.Add(kli)
        Next
        If CamerasList_KLB.Items.Count > 0 Then
            SelectListItem()
        Else
            RaiseEvent CameraListEmpty(Me, Nothing)
        End If
        Me.ResumeLayout()
    End Sub
    Private Sub ViewJCList()
        Me.SuspendLayout()
        CamerasList_KLB.Items.Clear()
        For Each jci As JpegCameraInfos In _JCList
            Dim kli As New KryptonListItem(jci.CameraName, "", My.Resources.Img_Webcam)
            CamerasList_KLB.Items.Add(kli)
        Next
        If CamerasList_KLB.Items.Count > 0 Then
            SelectListItem()
        Else
            RaiseEvent CameraListEmpty(Me, Nothing)
        End If
        Me.ResumeLayout()
    End Sub
    Private Sub ViewMCList()
        Me.SuspendLayout()
        CamerasList_KLB.Items.Clear()
        For Each mci As MjpegCameraInfos In _MCList
            Dim kli As New KryptonListItem(mci.CameraName, "", My.Resources.Img_Webcam)
            CamerasList_KLB.Items.Add(kli)
        Next
        If CamerasList_KLB.Items.Count > 0 Then
            SelectListItem()
        Else
            RaiseEvent CameraListEmpty(Me, Nothing)
        End If
        Me.ResumeLayout()
    End Sub
    'Select List Item :
    Private Sub SelectListItem(Optional ByVal index As Integer = 0)
        If CamerasList_KLB.Items.Count > 0 Then
            CamerasList_KLB.SelectedItem = Nothing
            CamerasList_KLB.SelectedItem = CamerasList_KLB.Items.Item(index)
        End If
    End Sub
    'Set Actions :
    Private Sub SetLCActions()
        Me.SuspendLayout()
        HeaderTSB_Reload.ToolTipText = "Reload Local Cameras List"
        HeaderTSB_AddWebCam.Visible = False
        HeaderTSB_DelWebCam.Visible = False
        HeaderTSB_SaveWebcamsList.Visible = False
        Me.ResumeLayout()
    End Sub
    Private Sub SetJCActions()
        HeaderTSB_Reload.ToolTipText = "Reload Jpeg Cameras List"
        HeaderTSB_AddWebCam.Visible = True
        HeaderTSB_DelWebCam.Visible = True
        HeaderTSB_SaveWebcamsList.Visible = True
        HeaderTSB_AddWebCam.ToolTipText = "Add Jpeg Camera"
        HeaderTSB_DelWebCam.ToolTipText = "Delete Jpeg Camera"
        HeaderTSB_SaveWebcamsList.ToolTipText = "Save Jpeg Cameras List"
        If CamerasList_KLB.Items.Count > 0 Then
            HeaderTSB_DelWebCam.Enabled = True
        Else
            HeaderTSB_DelWebCam.Enabled = False
        End If
    End Sub
    Private Sub SetMCActions()
        HeaderTSB_Reload.ToolTipText = "Reload Mjpeg Cameras List"
        HeaderTSB_AddWebCam.Visible = True
        HeaderTSB_DelWebCam.Visible = True
        HeaderTSB_SaveWebcamsList.Visible = True
        HeaderTSB_AddWebCam.ToolTipText = "Add Mjpeg Camera"
        HeaderTSB_DelWebCam.ToolTipText = "Delete Mjpeg Camera"
        HeaderTSB_SaveWebcamsList.ToolTipText = "Save Mjpeg Cameras List"
        If CamerasList_KLB.Items.Count > 0 Then
            HeaderTSB_DelWebCam.Enabled = True
        Else
            HeaderTSB_DelWebCam.Enabled = False
        End If
    End Sub

    'Header TSBs :
    Private Sub HeaderTSB_Local_Click(sender As Object, e As System.EventArgs) Handles HeaderTSB_Local.Click
        SelectTSB(HeaderTSB_Local)
        SetLCActions()
        ViewLCList()
    End Sub
    Private Sub HeaderTSB_Jpeg_Click(sender As Object, e As System.EventArgs) Handles HeaderTSB_Jpeg.Click
        SelectTSB(HeaderTSB_Jpeg)
        SetJCActions()
        ViewJCList()
    End Sub
    Private Sub HeaderTSB_Mjpeg_Click(sender As Object, e As System.EventArgs) Handles HeaderTSB_Mjpeg.Click
        SelectTSB(HeaderTSB_Mjpeg)
        SetMCActions()
        ViewMCList()
    End Sub

    'Events :
    Public Event CameraListEmpty As EventHandler(Of EventArgs)
    Public Event CameraInfosSelected As EventHandler(Of CameraInfosSelectedEventArgs)
    Private Sub CamerasList_KLB_SelectedItemChanged(sender As Object, e As System.EventArgs) Handles CamerasList_KLB.SelectedValueChanged
        If CamerasList_KLB.SelectedItem IsNot Nothing Then
            Dim ici As ICameraInfos
            If HeaderTSB_Local.Checked = True Then
                ici = _LCList.Item(CamerasList_KLB.SelectedIndex)
            ElseIf HeaderTSB_Jpeg.Checked = True Then
                ici = _JCList.Item(CamerasList_KLB.SelectedIndex)
            ElseIf HeaderTSB_Mjpeg.Checked = True Then
                ici = _MCList.Item(CamerasList_KLB.SelectedIndex)
            Else
                ici = Nothing
            End If
            RaiseEvent CameraInfosSelected(Me, New CameraInfosSelectedEventArgs(ici))
        End If
    End Sub

    'Actions :
    Private Sub HeaderTSB_Reload_Click(sender As System.Object, e As System.EventArgs) Handles HeaderTSB_Reload.Click
        If HeaderTSB_Local.Checked = True Then
            LoadLCList()
            HeaderTSB_Local.PerformClick()
        ElseIf HeaderTSB_Jpeg.Checked = True Then
            LoadJCList()
            HeaderTSB_Jpeg.PerformClick()
        ElseIf HeaderTSB_Mjpeg.Checked = True Then
            LoadMCList()
            HeaderTSB_Mjpeg.PerformClick()
        End If
    End Sub
    Private Sub HeaderTSB_AddWebCam_Click(sender As System.Object, e As System.EventArgs) Handles HeaderTSB_AddWebCam.Click
        If HeaderTSB_Jpeg.Checked = True Then
            _JCList.Add(New JpegCameraInfos("http://"))
            ViewJCList()
            SetJCActions()
            SelectListItem(_JCList.Count - 1)
        ElseIf HeaderTSB_Mjpeg.Checked = True Then
            _MCList.Add(New MjpegCameraInfos("http://"))
            ViewMCList()
            SetMCActions()
            SelectListItem(_MCList.Count - 1)
        End If
    End Sub
    Private Sub HeaderTSB_DelWebCam_Click(sender As System.Object, e As System.EventArgs) Handles HeaderTSB_DelWebCam.Click
        If HeaderTSB_Jpeg.Checked = True Then
            _JCList.RemoveAt(CamerasList_KLB.SelectedIndex)
            ViewJCList()
            SetJCActions()
            SelectListItem()
        ElseIf HeaderTSB_Mjpeg.Checked = True Then
            _MCList.RemoveAt(CamerasList_KLB.SelectedIndex)
            ViewMCList()
            SetMCActions()
            SelectListItem()
        End If
    End Sub
    Private Sub HeaderTSB_SaveWebcamsList_Click(sender As System.Object, e As System.EventArgs) Handles HeaderTSB_SaveWebcamsList.Click
        If HeaderTSB_Jpeg.Checked = True Then
            SaveJCList()
            LoadJCList()
            HeaderTSB_Jpeg.PerformClick()
        ElseIf HeaderTSB_Mjpeg.Checked = True Then
            SaveMCList()
            LoadMCList()
            HeaderTSB_Mjpeg.PerformClick()
        End If
    End Sub

End Class

Public Class CameraInfosSelectedEventArgs
    Inherits EventArgs
    Public Sub New(ici As ICameraInfos)
        _SelectedCameraInfos = ici
    End Sub
    Private _SelectedCameraInfos As ICameraInfos
    Public ReadOnly Property SelectedCameraInfos As ICameraInfos
        Get
            Return _SelectedCameraInfos
        End Get
    End Property
End Class
